package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Collection;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import activity.SetClockActivity;

/**
 * 
 * @author vincenzo.delia@polito.it
 *
 */
@SuppressWarnings("serial")
public class ToolsMenu extends HcsGuiMenu {

	private CommandMenuBar commandMenuBar;
	private JMenuItem start;
	private JMenuItem stop;
	private SetRoomParametersDialog srpDialog;
	
	public ActionListener StopTimer = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			start.setEnabled(true);
			stop.setEnabled(false);
			SetClockActivity s = new SetClockActivity();
			s.stop();
		}
	};
	
	public ActionListener StartTimer = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			start.setEnabled(false);
			stop.setEnabled(true);
			SetClockActivity s = new SetClockActivity();
			s.start();
		}
	};
	
	public ActionListener SetClock = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			MainFrame frame = commandMenuBar.getMainFrame();
			SetClockActivity clk = new SetClockActivity();
			String msgs[] = {new String("Current clock is " + clk.geDelay() + "[ms]"),
					new String("Set new clock")};
			String resp = JOptionPane.showInputDialog(msgs);
			if(resp == null) return;
			try {
				int newclk = Integer.parseInt(resp);
				if(newclk < 0) throw new NumberFormatException();
				clk.setDelay(newclk);
			}
			catch(NumberFormatException nfex) {
				JOptionPane.showMessageDialog(frame,resp + ": Invalid value", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	};


	public ActionListener SelectAllMonitors;
	public ActionListener DeSelectAllMonitors;
	
	/**
	 * 
	 * @param select
	 * @return an ActionListener which selects/deselects 
	 * {@link RoomMonitor}s
	 */
	private ActionListener buildSelector(final boolean select) {
		ActionListener ret = new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				MainFrame frame = commandMenuBar.getMainFrame();
				List<RoomMonitor> list = frame.getRoomMonitors();
				for(RoomMonitor m : list) {
					if(select != m.isSelected()) {
						m.mouseListener.mouseClicked(null);
					}
				}
			}
		};
		return ret;
	}
	
	public ActionListener SetRoomParameter = new ActionListener() {

		public void actionPerformed(ActionEvent e) {
			MainFrame frame = commandMenuBar.getMainFrame();
			Collection<RoomMonitor> rms = frame.getRoomMonitors();
			boolean anySelected = false;
			for(RoomMonitor rm : rms) {
				anySelected = rm.isSelected();
				if(anySelected) break;
			}
			if(anySelected)
				srpDialog.setVisible(true);
			else
				JOptionPane.showMessageDialog(frame, "Select a room", "Warning", JOptionPane.WARNING_MESSAGE);
		}
	};
	
	public ToolsMenu(CommandMenuBar commandMenuBar) {
		super("Tools");
		this.commandMenuBar = commandMenuBar;
		this.srpDialog = new SetRoomParametersDialog(this);
		SelectAllMonitors = buildSelector(true);
		DeSelectAllMonitors = buildSelector(false);
		this.setMnemonic(KeyEvent.VK_T);
		start = GuiUtilities.fitMenuItem(this,"Start", StartTimer, false, KeyEvent.VK_1);
		GuiUtilities.fitMenuItem(this,"Set Clock...",SetClock, true,KeyEvent.VK_C);
		stop = GuiUtilities.fitMenuItem(this,"Stop", StopTimer, true,KeyEvent.VK_0);
		
		this.addSeparator();
		
		GuiUtilities.fitMenuItem(this,"Select All",SelectAllMonitors, true,KeyEvent.VK_S);
		GuiUtilities.fitMenuItem(this,"Deselect All",DeSelectAllMonitors, true,KeyEvent.VK_D);
		
		this.addSeparator();
		
		GuiUtilities.fitMenuItem(this,"Settings...",SetRoomParameter, true,KeyEvent.VK_E);
		
		
	}

	@Override
	public CommandMenuBar getCommandMenuBar() {
		return this.commandMenuBar;
	}
	
	
}
